from datetime import datetime
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

from FileShare.extensions import db

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(20), unique=True, index=True)
    email = db.Column(db.String(254), unique=True, index=True)
    password_hash = db.Column(db.String(128))
    phone = db.Column(db.String(11))
    cdatetime = db.Column(db.DateTime, default=datetime.now)

    loginfo = db.relationship('loginfo')
    isdelete = db.Column(db.Boolean, default=False)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)
    
    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)

class loginfo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    ip_address = db.Column(db.String(15))
    ops_type = db.Column(db.String(255))
    filename = db.Column(db.String(255))
    opsdate = db.Column(db.DateTime, default=datetime.now)

